package test.pfsimulator

import pfsimulator.ParaflowSimulator
import pfsimulator.PNode
import pfsimulator.VNode
import pfsimulator.Flow
import java.util.List

import actors.{TIMEOUT, Actor}
import java.util

/**
 * Created with IntelliJ IDEA.
 * User: codefish
 * Date: 11/5/12
 * Time: 6:48 PM
 * To change this template use File | Settings | File Templates.
 */
object PNodeMonitor extends Actor{
    var pNode: PNode = null
    def act() {
        var tick = 0
        loop{
            reactWithin(1000){
                case TIMEOUT =>
                    tick += 1
                    if (tick == 2){
                        // Add first flow
                        simulator.addFlow(new Flow(10, v1, v2, oneGige, 10*oneGige));
                        simulator.startFlow(10);
                        // Add the second flow
                        simulator.addFlow(new Flow(20, v1, v2, 512*oneMege, 5*oneGige));
                    } else if (tick == 7){
                        simulator.throttle(10, 512*oneMege);
                        simulator.startFlow(20);
                    } else if (tick == 18){
                        simulator.delFlow(10);
                        simulator.delFlow(20);
                    }

                    simulator.printState(pNode)
            }
        }
    }

    def setPNode(pNode: PNode){
        this.pNode = pNode
    }

    val oneGige = (1024*1024*1024).toLong
    val oneMege = (1024*1024).toLong
    var simulator: ParaflowSimulator = null

    simulator = new ParaflowSimulator()
    val p1 = new PNode(1,oneGige)
    val p2 = new PNode(2,oneGige)
    val v1 = new VNode("host1", "0.0.0.1")
    val v2 = new VNode("host2", "0.0.0.2")
    p1.addVNode(v1)
    p2.addVNode(v2)
    var pNodeList: List[PNode]  = new util.LinkedList[PNode]()
    pNodeList.add(p1)
    pNodeList.add(p2)
    simulator.construct(pNodeList)
    // start monitors
    this.setPNode(p1)

    def main(args: Array[String]) {
        this.start
    }
}
